package com.example.movie.utils

import android.annotation.SuppressLint
import android.os.Build
import androidx.annotation.RequiresApi
import java.text.SimpleDateFormat
import java.util.*


/**
 * 日期格式化工具
 *
 * @author swift
 */
object DateUtil {


    /**
     * 格式化时间格式
     */
    @RequiresApi(Build.VERSION_CODES.O)
    @SuppressLint("SimpleDateFormat")
    fun DurationOfPlayTime(dateTime: String): String {
        val beginTime = SimpleDateFormat("yyyy-MM-dd hh:mm").parse(dateTime)
        val now = Date()
        if (now.time > beginTime.time) {
            return "电影票已经过期"
        }

        var last = beginTime.time - now.time
        val days = last / (1000 * 60 * 60 * 24)
        last -=  days * (1000 * 60 * 60 * 24)
        val hours = last / (1000 * 60 * 60)
        last -=  hours * (1000 * 60 * 60)
        val minutes = last / (1000 * 60)

        with(StringBuilder()) {

            if (days > 0) {
                this.append(days).append("天")
            }
            if (hours > 0) {
                this.append(hours).append("时")
            }
            if (minutes > 0) {
                this.append(minutes).append("分")
            }

            return this.toString()
        }
    }

    fun parseDate(date:String,pattern:String): Long {
        return SimpleDateFormat(pattern).parse(date).time
    }

}

